package com.fand.common.controller;

import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.fand.acceptControl.dto.NavigationDto;
import com.fand.common.service.CommonService;

/**
 * @author yaoHT
 */
@Controller
public class AdminController {
	/**
	 * 公共服务
	 */
	@Qualifier(CommonService.BEAN_ID)
	@Autowired(required = false)
	private CommonService commonService;
	
	@RequestMapping(value = { "/admin/index.htm", "/admin.htm" }, method = RequestMethod.GET)
	public String adminIndex() {
		return "admin/index";
	}
	
	@RequestMapping(value = { "/admin/leftNav.htm" }, method = RequestMethod.POST)
	public String adminLeftNav(final NavigationDto selectNavigationBy, final Model model) {
		final List<NavigationDto> navigationList = this.commonService.getAcceptControlService()
		        .getNavigationService().selectNavigationBy3ForMappedObjectList(selectNavigationBy, 2);
		
		model.addAttribute("navigationList", navigationList);
		
		return "admin/leftNav";
	}
	
	@RequestMapping(value = { "/admin/topNav.htm" })
	public String adminTopNav(final NavigationDto selectNavigationBy, final Model model) {
		final List<NavigationDto> navigationList = this.commonService.getAcceptControlService()
		        .getNavigationService().selectNavigationBy2ForMappedObjectList(selectNavigationBy);
		
		if (StringUtils.isBlank(selectNavigationBy.getId())) {
			if (!CollectionUtils.isEmpty(navigationList)) {
				final String id = navigationList.get(0).getId();
				
				selectNavigationBy.setId(id);
			}
		}
		
		model.addAttribute("navigationList", navigationList);
		
		model.addAttribute("id", selectNavigationBy.getId());
		
		return "admin/topNav";
	}
}
